const arr = [
  {id: 1},
  {id: 2, before: 1},
  {id: 3, after: 1},
  {id: 5, first: true},
  {id: 6, last: true},
  {id: 7, after: 8},
  {id: 8},
  {id: 9}
]

function Node(id) {
  this.id = id
  this.before = null
  this.after = null
}

function insert(node, newNode) {
  // todo 处理逻辑不对
  if (newNode.before && newNode.before === node.id) {
    node.after = newNode
  } else if (newNode.after && newNode.after === node.id) {
    node.before = newNode
  }
}

function order(arr) {
  let root = null
  function makeTree(id) {
    const newNode = new Node(id)
    if (root === null) {
      root = newNode
    } else {
      insert(root, newNode)
    }
  }

  arr.forEach(item => makeTree(item.id))
  console.log(root)
  return root
}

order(arr)